<?php 

Class adModel extends AppModel
{
	
	public function getImg($id)
	{
		$sql="SELECT * FROM ".$this->table('block_content')." WHERE id = '".$id."'";
			
		$result=$this->getRow($sql);
		if($result)
		{
			$arr3=hy_dig_filter($result['title'],3);
			$result['en_title']=$arr3[1];
			$result['title']=$arr3[0];
			
			$arr=hy_dig_filter($result['description'],3);
			$result['en_description']=$arr[1];
			$result['description']=$arr[0];
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	public function get_group_id($id)
	{
		$sql="SELECT block_id FROM ".$this->table('block_content')." WHERE id=".$id;
		return $this->getOne($sql);
	}
	
	public function del_group($id)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
				
			$sql="DELETE FROM ".$this->table('block')." WHERE  id='".$id."'";
	
			$res=$this->query($sql);
				
			$sql="DELETE FROM ".$this->table('block_content')." WHERE  block_id='".$id."'";
	
			$res2=$this->query($sql);
			if(!$res||!$res2)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
			
	
				
	
	
	
		}
		return $result;
	}
	public function del_img($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
	
			
	
			$sql="DELETE FROM ".$this->table('block_content')." WHERE  id='".$id."'";
	
			$res2=$this->query($sql);
			if(!$res2)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
	
	
	
	
	
	
		}
		return $result;
	}
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('block_content')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	public function add_images($info)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
		
		
		if($info['pic_path'])
		{
			
			foreach($info as $k=>$v)
			{
				if($v!='')
				{
					$field.="$k,";
						
						
					$value.="'$v',";
			
				}
			}
			
			$field.="created";
			$value.="'".$_SERVER['REQUEST_TIME']."'";
			
			$sql="INSERT INTO ".$this->table('block_content')." (".$field.") VALUES(".$value.")";
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
		}
		else 
		{
			$result['error']=2;
			$result['content']=sprintf(Lang('UPLOAD_FAILURE'),$files['name']);
		}
		return $result;
		//$this->make_thumb('/Uploads/ad/1398673595657549402.jpg','200','200');
	}
	public function ad_list($page=1)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		
		$result=$this->show_page("SELECT id,title,code,created,description FROM ".$this->table('block')." ORDER BY id DESC",$page);
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
		
		
		return $listPage;
	}
	public function search_ad($page=1,$keyword)
	{
		$listPage=array('data'=>'','pageLink'=>'');
	
		$result=$this->show_page("SELECT id,title,code,created FROM ".$this->table('block')." WHERE title like '%".$keyword."' OR title like '".$keyword."%'",$page);
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
	
	
		return $listPage;
	}
	public function content_list($page=1,$flash_id)
	{
		$listPage=array('data'=>'','pageLink'=>'');
	   if($flash_id)
	   {
		
		$result=$this->show_page("SELECT id,title,ordering,created FROM ".$this->table('block_content')." WHERE block_id='".$flash_id."' ORDER BY id DESC",$page);
		if($result)
		{
		foreach($result['result'] as  $k=>$v)
		{
			$result['result'][$k]['title']=hy_dig_filter($v['title'],1);
			
				
		}
		}
		
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
	   }
	
		return $listPage;
	}
	
	public function getGroup($id)
	{
		$sql="SELECT * FROM ".$this->table('block')." WHERE id = '".$id."'";
			
		$result=$this->getRow($sql);
		if($result)
		{
			//获取相册集
			$sql="SELECT p.* FROM ".$this->table('block_content')." as p WHERE p.flash_id = '".$id."'";
			
			$photo=$this->getAll($sql);
			foreach($photo as $k=>$v)
			{
				$arr=hy_dig_filter($v['title'],3);
				$photo[$k]['title']=$arr[0];
			}
			$result['photo']=$photo;
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	public function check_code($code,$id=0)
	{
		if($id>0)
		{
			$sql="SELECT code FROM ".$this->table('block')." WHERE id='".$id."'";
			$oldcode=$this->getOne($sql);
			$code_exist=0;
			if($oldcode!=$code)
			{
				$sql="SELECT * FROM ".$this->table('block')." WHERE code='$code'";
				$code_exist=$this->getOne($sql);
					
			}
			
			if($code_exist>0)
			{
				
				return false;
			}
			else 
			{
				return true;
			}
		}
		else 
		{
		    $sql="SELECT count(*) FROM ".$this->table('block')." WHERE code='$code'";
		    if($this->getOne($sql)>0)
		   {
			return false;
		   }
		   else 
		  {
			return true;
		  }
		}
	}
	public function update_group($info,$id)
	{
		if($id)
		{
			foreach($info as $k=>$v)
			{
				if($v!='')
				{
					$where.=" $k='$v',";
						
				}
			}
			$where.="modified=".$_SERVER['REQUEST_TIME'];
			$sql="UPDATE ".$this->table('block')." SET ".$where." WHERE id='".$id."'";
			return $res=$this->query($sql);
		}
	}
}